#ifndef MSGGEN_H
#define MSGGEN_H

class MsgOutlets;
class MsgPlugs;
class MsgCompiled;

class MsgGenerator
{
	public:
#ifdef USE_MSGGENCACHE	// not thread-safe
		MsgGenerator(int32 maxMsgs, MsgOutlets*);
#else
		MsgGenerator(MsgOutlets*);
#endif
		~MsgGenerator();

		//
		// srcId uniquely identifies each src msg and ranges from 0 to maxMsgs-1
		//
		// The result (dest) can either be allocated by the caller
		// (genMsg) or by the MsgGenerator (genMsgAlloc).
		//
		// genMsg() will truncated to destMax bytes and will always return
		// a vaild msg (possibly only '\0').
		//
		// genMsgAlloc() may return *dest as the null pointer (and *destLen==0).
		//
		// The caller is responsible for delete[]-ing all memory allocated by
		// genMsgAlloc().
		//

#ifdef USE_MSGGENCACHE	// not thread-safe
		void genMsg(
			int32 srcId, char* src, int32 srclen, uint8 srcMD4[16],
			MsgPlugs*,
			char* dest, int32* destLen, int32 destMaxPlusNullTerm);

		void genMsgAlloc(
			int32 srcId, char* src, int32 srclen, uint8 srcMD4[16],
			MsgPlugs*,
			char** dest, int32* destLen);
#else
		void genMsg(
			char* src, int32 srclen,
			MsgPlugs*,
			char* dest, int32* destLen, int32 destMaxPlusNullTerm);

		void genMsgAlloc(
			char* src, int32 srclen,
			MsgPlugs*,
			char** dest, int32* destLen);
#endif

	protected:
#ifdef USE_MSGGENCACHE	// not thread-safe
		MsgCompiled* compile(int32, char*, int32, uint8 [16]);
#else
		MsgCompiled* compile(char*, int32);
#endif
		void plugEmIn(MsgCompiled*, char*, int32, MsgPlugs*,
			char*, int32*, int32);
		bool8 copy(char**, int32*, char*, int32);

	protected:
#ifdef USE_MSGGENCACHE	// not thread-safe
		int32			numCompiled;
		MsgCompiled**	compiled;
#endif
		MsgOutlets*		outlets;
};

#endif
